package org.rich.charlesmurphy.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.rich.charlesmurphy.model.CharlieMurphyModel;
import org.rich.charlesmurphy.model.Resource;
import org.rich.charlesmurphy.model.ResourceType;
import org.windowkit.toolkit.layout.GridGenerator;
import org.windowkit.toolkit.panel.AbstractPanel;

/**
 *
 * @author Rich
 */
public class ControlPanel extends AbstractPanel {
	private static final long serialVersionUID = 1111453042310665563L;

	private JTextField txtFile;
	private JTextField txtTimeSpan;
	private JTextField txtTotal;

	private CharlieMurphyModel model;


	private Map<ResourceType, JTextField> totalViews;
	private Map<ResourceType, JTextField> distinctViews;

	public ControlPanel(CharlieMurphyModel model) {
		super(model.getSessionName());
		setModel(model);
	}

	@Override
	protected String getLayoutPosition() {
		return BorderLayout.NORTH;
	}

	@Override
	protected JComponent createContents() {

		GridGenerator gg = new GridGenerator(3);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(3, 3, 3, 3);
		gbc.weightx = 0;
		gbc.fill = GridBagConstraints.NONE;
		gg.addConstraintsForColumn(0, gbc);



		gg.getCompConstraints().weightx = 1;
		gg.getCompConstraints().weighty = 0;
		gg.getCompConstraints().insets = new Insets(3, 3, 3, 3);
		gg.getLabelConstraints().weightx = 0;
		gg.getLabelConstraints().weighty = 0;
		gg.getLabelConstraints().insets = new Insets(3, 3, 3, 3);
		gg.getLabelConstraints().anchor = GridBagConstraints.WEST;
		gg.getLabelConstraints().fill = GridBagConstraints.NONE;


		gg.add(txtFile, "File: ");

		gg.add(txtTimeSpan, "Time span: ");

		gg.newLine();
		gg.add(new JLabel(" - Totals- "));
		gg.newLine();
		gg.add(makeAllButton(null));
		gg.add(txtTotal, "Requests: ");

		for (Entry<ResourceType, JTextField> pair : totalViews.entrySet()) {
			gg.add(makeAllButton(pair.getKey()));
			gg.add(pair.getValue(), pair.getKey().display() + ": ");
		}

		gg.add(new JLabel(" - Distinct totals -"));
		gg.newLine();
		for (Entry<ResourceType, JTextField> pair : distinctViews.entrySet()) {
			gg.add(makeDistinctButton(pair.getKey()));
			gg.add(pair.getValue(), pair.getKey().display() + ": ");
		}
		

		return gg.getPanel();
	}
	@Override
	protected void initComponents() {
		ResourceType[] resourceTypes = ResourceType.values();
		totalViews = new TreeMap<ResourceType, JTextField>();

		for (ResourceType rt : resourceTypes) {
			totalViews.put(rt, makeField());
		}

		distinctViews = new TreeMap<ResourceType, JTextField>();
		for (ResourceType rt : resourceTypes) {
			distinctViews.put(rt, makeField());
		}

		txtFile = makeField();
		txtTimeSpan = makeField();
		txtTotal = makeField();
	}

	JTextField makeField() {
		return makeField(null);

	}
	JTextField makeField(String text) {
		JTextField tf = new JTextField(10);
		tf.setEditable(false);
		tf.setBorder(null);
		if (text != null) {
			tf.setText(text);
		}
		return tf;
	}

	@Override
	public Dimension requestDimesions() {
		return new Dimension(280, 500);
	}


	JButton makeButton(ActionListener al) {
		JButton but = new JButton("View");
		but.setSize(30, 15);
		but.addActionListener(al);
		return but;

	}

	JButton makeAllButton(final ResourceType type) {
		return makeButton(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ViewByRequest view;
				if (type == null) {
					view = new ViewByRequest("All");
					view.setModel(model.getCoreData());
				}
				else {
					view = new ViewByRequest("All " + type.display());
					view.setModel(model.getRequestsByType(type));
				}
				getLauncher().launchWindow(view);
			}
		});
	}
	JButton makeDistinctButton(final ResourceType type) {
		return makeButton(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = "Distinct " + (type == null ? "All" : type.display());
				ViewByResource view = new ViewByResource(name);
				view.setModel(model.getResourcesByType(type));
				getLauncher().launchWindow(view);
			}
		});
	}

	public void setModel(CharlieMurphyModel model) {
		this.model = model;
		txtFile.setText(model.getSessionName());
		txtTimeSpan.setText(model.getTimeSpan());

		txtTotal.setText(String.valueOf(model.getCoreData().size()));

		for(Entry<ResourceType, JTextField> pair : totalViews.entrySet()) {
			pair.getValue().setText(String.valueOf(model.calculateTotalRequests(pair.getKey())));
		}
		for(Entry<ResourceType, JTextField> pair : distinctViews.entrySet()) {
			pair.getValue().setText(String.valueOf(model.calculateDistinctRequests(pair.getKey())));
		}

	}

	public void launchViewRequestGroupsForResource(Resource resource) {
		getLauncher().launchWindow(new ViewRequestGroups(
				resource.getPath(),
				model.findGroupsThatRequestedResource(resource)));
	}

}
